﻿#region using
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
#endregion

namespace AwesomeTanks.Sources.GameElements.MapElements
{
    /// <summary>
    /// The model of a Map in AwesomeTanks
    /// </summary>
    public class Map
    {
        #region Fields
        public List<Wall> ListWall { get; set; }
        public List<SpawnPoint> ListSpawn { get; set; }
        public string MapName { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="Map"/> class.
        /// </summary>
        /// <param name="listwall">The listwall.</param>
        /// <param name="listspawn">The listspawn.</param>
        public Map() : this(new List<Wall>(), new List<SpawnPoint>())
        {
        }


        public Map(List<Wall> listwall, List<SpawnPoint> listspawn)
        {
            ListWall = listwall;
            ListSpawn = listspawn;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds a wall to the Map.
        /// </summary>
        /// <param name="wall">The wall.</param>
        public void AddWall(Wall wall)
        {
            ListWall.Add(wall);
        }

        /// <summary>
        /// Deteles a wall from the Map.
        /// </summary>
        /// <param name="wall">The wall.</param>
        public void DeleteWall(Wall wall)
        {
            ListWall.Remove(wall);
            wall = null;
        }

        /// <summary>
        /// Adds a Spawn Point to the Map.
        /// </summary>
        /// <param name="spawnPoint">The spawn point.</param>
        public void AddSpawn(SpawnPoint spawnPoint)
        {
            ListSpawn.Add(spawnPoint);
        }

        /// <summary>
        /// Gets the Spawn Point related to this PlayerIndex.
        /// </summary>
        /// <param name="indexPlayer">The index player.</param>
        /// <returns></returns>
        public Vector2 getSpawn(PlayerIndex indexPlayer)
        {
            SpawnPoint spawn = null;
            for (int i = 0; i < ListSpawn.Count;i++ )
            {
                if (ListSpawn[i].IndexPlayerSpawn == indexPlayer)
                {
                    spawn = ListSpawn[i];
                }
            }
            return spawn.SpawnPosition;
        }


        public void SetLifeDestructibleWall(int life)
        {
            foreach(Wall wall in ListWall)
            {
                if (wall.MaxHealth != null)
                {
                    wall.MaxHealth = life;
                    wall.Health = life;
                }
            }
        }

        #endregion

        #region Update and Draw

        /// <summary>
        /// Updates the Map at the specified gametime.
        /// </summary>
        /// <param name="gametime">The gametime.</param>
        public void Update(GameTime gametime)
        {
            for (int index = 0; index < ListWall.Count;index++ )
            {
                //Update each wall
                ListWall[index].Update(gametime);
            }
        }

        /// <summary>
        /// Draws the Map with specified sprite batch.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Wall wall in ListWall)
            {
                //Draw each wall
                wall.Draw(spriteBatch);
            }
        }
        #endregion
    }
}
